angular.module('myApp.pages.score.company.target', [])
// 定义路由
      .config(['$routeProvider', function ($routeProvider) {
            $routeProvider.when('/score/company/target/:id/:name', {
                  templateUrl: 'pages/score/company/detail/target.html',
                  controller: 'ScoreCompanyTargetCtrl'
            })
      }])
      // 自定义控制器
      .controller('ScoreCompanyTargetCtrl', function ($scope,$toast, $CONST, $http, $routeParams, $location,$interval) {
            //弹出窗，提示
            // 不展示弹出框
            $scope.show = false;
            $scope.title = '提示';
            $scope.list = [{
                  type: 'toast', value: '确定上传'
            }];
            // 左侧菜单集合
            $scope.menuList = [];
            // 默认展示第一个左侧菜单
            $scope.currentIndex = 0;
            // 从session中获取financialIndex
            $scope.allFinancialIndex = session.get('allFinancialIndex');
            //财务指标下的得分
            $scope.financialIndex = $scope.allFinancialIndex.noFinancialIndexList[0];
            //非财务指标下的菜单
            $scope.unFinancialIndex = $scope.allFinancialIndex.noIndexListNew;
            //非财务指标下的左侧菜单
            $scope.menuList = $scope.unFinancialIndex;


            // 右侧内容
            $scope.titieList = [];

            $scope.radio = '';

            angular.forEach($scope.menuList, function (menu) {
                  $scope.item = menu;
                  $scope.titieList.push($scope.item);
            });

            $scope.selectedMenuList = [
                  {
                        "personQuality": {
                              "quality": 0,
                              "creditRecord": 0,
                              "management": 0,
                              "operationTime": 0,
                              "education": 0,
                              "health": 0
                        }
                  },
                  {
                        "goodsAttribute": {
                              "channelLevel": 0,
                              "goodsType": 0,
                              "brandPopularity": 0,
                              "shelfLife": 0
                        }
                  },
                  {
                        "repayment": {
                              "cumulativeInventoryRatio": 0,
                              "guarantee": 0,
                              "receivable": 0
                        }
                  },
                  {
                        "companyQuality": {
                              "registeredCapital": 0,
                              "managementTeam": 0,
                              "workerQuality": 0,
                              "marketCompete": 0,
                              "operateYears": 0,
                              "comCreditRecord": 0,
                              "customerCooperation": 0
                        }
                  },
                  {
                        "commonlyTrade": {
                              "sustainable": 0,
                              "salseGrowth": 0
                        }
                  },
                  {
                        "wholesaleTrade": {
                              "marketManage": 0,
                              "sustainable": 0,
                              "jdSaleChannel": 0
                        }
                  },
                  {
                        "makePolicy": {
                              "industryPolicy": 0,
                              "environment": 0,
                              "development": 0
                        }
                  },
                  {
                        "makeIndex": {
                              "sustainable": 0,
                              "deviceStart": 0
                        }
                  },
                  {
                        "retailTrade": {
                              "sustainable": 0,
                              "locationAdvantage": 0
                        }
                  }
            ];

            /**
             * 处理选择的菜单
             *
             * @param { Object } menu 当前点击的菜单
             * @param { Object } name 当前参数的名称
             */
            $scope.handlerCheckedMenu = function (menu, name, obj) {
                  angular.forEach($scope.selectedMenuList, function (firstMenu) {
                        // 如果当前属性中包括次字段
                        if (firstMenu[$scope.menuList[$scope.currentIndex].e_name]) {
                              firstMenu[$scope.menuList[$scope.currentIndex].e_name][name] = parseInt(menu.score);
                        }
                  });
            };

            $scope.handlerCurrentTarget = function ($index, eName) {
                  $scope.currentIndex = $index;
            };


            //用户点击提交按钮，弹出提示框
            $scope.showTip = function () {
                  $scope.show = true;
            }


            //提交分数
            $scope.submitScore = function () {
                  //默认未选中无法上传
                  var stop=0;

                  $('.che').each(function (i) {
                        if($(this).val() != ''){
                              stop++
                        };
                        if ($(this).val() == '') {
                              stop=stop+0;
                        }
                  });
                  if(stop == $('.che').length){
                        $http({
                               method:'POST',
                               url:$CONST.$IP+'score/v1/addCompanyOtherIndex',
                               data:{
                               companyId:$routeParams.id,
                               companyName: decodeURIComponent($routeParams.name),
                               otherIndex:$scope.selectedMenuList
                               }
                               }).then(function successCallback(response) {
                              $toast('上传成功');
                                    $location.path('/score/company/analysis/'+ $routeParams.id + '/' + decodeURIComponent($routeParams.name));

                               }, function errorCallback(response) {
                                     $toast('上传失败');
                                     $scope.show = false;
                               });
                  }else{
                        $toast('非财务指标未填写完整');
                        $scope.show = false;
                        return ;
                  }
            }


      });